﻿using DbSync;
using DBSync.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DBSync.wpfPage
{
    /// <summary>
    /// Interaction logic for LoginSettingsPage.xaml
    /// </summary>
    public partial class LoginSettingsPage : Page
    {
        private static LoginSettingsPage instance;
        public static object FromPage { get; set; }

        public static LoginSettingsPage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LoginSettingsPage();
                }

                return instance;
            }
        }

        public LoginSettingsPage()
        {
            InitializeComponent();
        }



        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            if (SessionData.user != null)
            {
                lblUsername.Content = SessionData.user.USERNAME.Trim();
                txtCurrentPassword.Password = SessionData.user.PASSWORD.Trim();
                txtNewPassword.Password = "";
                txtConfirmPassword.Password = "";
            }
        }

        private void btnSettingsSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtCurrentPassword.Password == SessionData.user.PASSWORD.Trim())
            {
                if (txtNewPassword.Password != "")
                {
                    if (txtNewPassword.Password == txtConfirmPassword.Password)
                    {
                        syncdbEntities db = new syncdbEntities();
                        tbl_user user = db.tbl_user.Where(U => U.ID == SessionData.user.ID).SingleOrDefault();
                        user.PASSWORD = txtNewPassword.Password;
                        int status = db.SaveChanges();
                        if (status > 0)
                        {
                            SessionData.user.PASSWORD = user.PASSWORD;
                            MSGWindow msg = new MSGWindow("Success", "Your password has been updated successfully");
                            msg.ShowDialog();
                            
                            if (FromPage != null)
                            {
                                MainWindow.Instance.ContentFrame.Content = FromPage;
                            }

                        }
                        else
                        {
                            MSGWindow msg = new MSGWindow("Error", "Problem occured while changing password. Please try again");
                            msg.ShowDialog();
                        }
                    }
                    else
                    {
                        MSGWindow msg = new MSGWindow("Error", "Password and Confirmation does not match");
                        msg.ShowDialog();
                    }
                }
                else
                {
                    MSGWindow msg = new MSGWindow("Error", "Please provide valid password");
                    msg.ShowDialog();
                }
            }
            else
            {
                MSGWindow msg = new MSGWindow("Error", "Current password provided is incorrect");
                msg.ShowDialog();
            }
        }

        private void btnSettingsReset_Click(object sender, RoutedEventArgs e)
        {
            lblUsername.Content = "";
            txtCurrentPassword.Password = "";
            txtNewPassword.Password = "";
            txtConfirmPassword.Password = "";
            if (FromPage != null)
            {
                MainWindow.Instance.ContentFrame.Content = FromPage;
            }
        }
    }
}
